<?php

/**f
 * becas actions.
 *
 * @package    auip
 * @subpackage becas
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class becasActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {   
      //$this->datos = $request->getParameter('v');
        $this->becas = new sfDoctrinePager('Beca', 5);
        $this->becas->setQuery(Doctrine::getTable('Beca')->createQuery('b')->where('b.registro_activo <> 0')->addOrderBy('b.fecha_inicio DESC')/*->andWhere( 'b.fecha_inicio >= ?',date('Y-m-d H:i:s', time() + 86400 * sfConfig::get('app_active_days')) )*/ );
        $this->becas->setPage($request->getParameter('pagina', 1));
        $this->becas->init();
  }
  
  public function executeShow(sfWebRequest $request)
  {
    $this->beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('id')));       
    $this->forward404Unless($this->beca);    
  }

  public function executeNew(sfWebRequest $request)
  {         
      $this->form = new BecaForm();      
  }
  
  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BecaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }
 
  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('id'))), sprintf('Object beca does not exist (%s).', $request->getParameter('id')));
    $this->form = new BecaForm($beca);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('id'))), sprintf('Object beca does not exist (%s).', $request->getParameter('id')));
    $this->form = new BecaForm($beca);    
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }
  
  public function executeDelete(sfWebRequest $request)
  {
//    $request->checkCSRFProtection();
//    $this->forward404Unless($beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('id'))), sprintf('Object beca does not exist (%s).', $request->getParameter('id')));
//    $beca->delete();
    $arr_use = $request->getPostParameter('becas');
    
    for($i=0; $i<count($arr_use); $i++)
    {
        print '-->'.$arr_use[$i];
        $this->forward404Unless($beca = Doctrine_Core::getTable('Beca')->find($arr_use[$i]), sprintf('El objeto beca NO EXISTE (%s).', $arr_use[$i]));
        $beca->setRegistroActivo(0);
        $beca->save();
    } 
    $this->redirect('becas/index');
  }

  /*FUNCION QUE PERMITE REVISAR EL FORMULARIO ANTES DE GUARDARLO*/
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));            
    $valor = $request->getParameter('adicion');
    
    if ($form->isValid())
    {   
       $file = $this->form->getValue('logo');
       $id_uni = $this->form->getValue('universidad_ofrece_id');   
       $id_beca = $this->form->getValue('id');
       $file->setNameFile('logo'.$id_uni);
       $nuevaBeca = Doctrine::getTable('Beca')->createQuery('b')->where('b.logo ILIKE ?', '%logo'.$id.'%')->execute()->toArray();                    
       
        //PERMITE ENCONTRAR SI EL LOGO YA SE ENCUENTRA INGRESADO
       $resultado = count($nuevaBeca);       
       if ($resultado === 1) 
       {
            $file->saveNoFile('logo'.$id.'.'.$file->getExtension());
            if ($valor == 1)
            {             

                $beca = $form->save();                                      
                $this->getUser()->setAttribute('beca_id', $beca->getId());
                $this->getUser()->setAttribute('nom', $beca->getNombre());           
                $this->redirect('atributos/new');
            }
            else
            {           
                $beca = $form->save();           
                $this->redirect('becas/index');        
            }
       }
       else
       {
           if ($valor == 1)
           {             
              $beca = $form->save();                      
              $this->getUser()->setAttribute('beca_id', $beca->getId());
              $this->getUser()->setAttribute('nom', $beca->getNombre());           
              $this->redirect('atributos/new');
           }
           else
           {           
               $beca = $form->save();           
               $this->redirect('becas/index');        
           }    
          }
       }
    
  }    
    
}
